
$.ajax({
    url: "/viptail/api/wxActivity/sysConfig",
    data: {
        url: location.href
    },
    type: "GET",
    dataType: "json",
    async: false,
    success: function(data) {
        if (data.respCode == '0') {
            if (location.href != data.url) {
                location.href = data.url;
            } else {
                if(isWechat()){
                    checkAuth();  
                }
            }
        }
    }
});
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|#|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r !== null)
        return decodeURIComponent(unescape(r[2]));
    return null;
}
function checkAuth() {

    var transfer = location.href.split('?')[0];

    // if (/share/.test(location.href)) {
    //     state = getUrlParam('state');
    // }
    var openid = sessionStorage.getItem("openid");
    if ( openid === null) {

        if (getUrlParam('code') == null) {
            console.log(transfer);
            location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaaecc90537464f29&redirect_uri=' + encodeURIComponent(transfer) +
                '&response_type=code&scope=snsapi_userinfo&state&#wechat_redirect';
        } else {
            $.ajax({
                url: "/viptail/api/wxActivity/getWxUserInfo",
                data: {
                    code: getUrlParam('code')
                },
                type: "GET",
                dataType: "json",
                async: false,
                success: function(data) {
                    if (data.wxuser.subscribe == 1) {
                        sessionStorage.setItem("openid", data.wxuser.openid); 
                        sessionStorage.setItem("userimg", data.wxuser.headimgurl); 
                        sessionStorage.setItem("nickName", data.wxuser.nickname); 
                        sessionStorage.setItem("unionid", data.wxuser.unionid); 
                    } else {
                        location.href = "./guanzhu.html";
                    }
                }
            });
        }
    } else {
        $.ajax({
            url: "/viptail/api/wxActivity/getWxUser",
            data: {
                openid: openid
            },
            type: "GET",
            dataType: "json",
            async: false,
            success: function(data) {
                if (data.wxuser.subscribe != 1) {
                    location.href = "./guanzhu.html";
                }   
            }
        });

    }
}
